package com.zcf.gameController.gameUtils;

import com.zcf.gameController.F_Websocket_s;
import com.zcf.gameController.WebSocketBull;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jj
 * @date 2019/11/26 9:12
 */
public class XtThread extends  Thread{

    private Util_Room uroom = new Util_Room();
    @Override
    public  void run() {
        System.out.println("---------------------开启心跳线程--------------------------");
        while (true ) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                List<RoomsParam>  values = new ArrayList<>(F_Websocket_s.rooms.values());
                for ( RoomsParam myRoom:values){
                    if (myRoom.getUsers().values().size()>0){
                        for (UserBean user:myRoom.getUsers().values()){
                            if (user.getXt()==0){
                                continue;
                            }
                            user.setXt(user.getXt()-1);
                            if (user.getXt()<=0){
                                System.out.println("____________________________"+user.getWxname()+"__________________________");
                                //玩家退出
                                uroom.exitGame(myRoom, user);
                                if (F_Websocket_s.clients.get(user.getId()+"")!=null) {
                                    F_Websocket_s f_websocket_s = F_Websocket_s.clients.get(user.getId() + "");
                                    f_websocket_s.disConnect(user.getId(),user.getSession());
                                }

                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                List<RoomsParam>  valuelist = new ArrayList<>(WebSocketBull.rooms.values());
                for ( RoomsParam myRoom:valuelist){
                    if (myRoom.getUsers().values().size()>0){
                        for (UserBean user:myRoom.getUsers().values()){
                            if (user.getXt()==0){
                                continue;
                            }
                            user.setXt(user.getXt()-1);
                            if (user.getXt()<=0){
                                System.out.println("____________________________"+user.getWxname()+"__________________________");
                                //玩家退出
                                RoomsParam roomsParam = WebSocketBull.rooms.get(myRoom.getRoomName());
                                uroom.exitGame(roomsParam, user);
                                if (WebSocketBull.clients.get(user.getId()+"")!=null) {
                                    WebSocketBull webSocketBull = WebSocketBull.clients.get(user.getId() + "");
                                    webSocketBull.disConnect(user.getId(),user.getSession());
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
